package com.medication.service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.google.common.collect.Lists;
import com.jfinal.plugin.activerecord.Db;
import com.medication.model.ThreeMedicationDir;
import com.medication.utils.ExcelExporter;
import com.medication.utils.HandlerExcelData;
import com.medication.utils.PinyinTool;

import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * @author heem
 * @create time 创建时间：2017年3月30日 下午4:43:02
 * @version 1.0
 * @since
 */
public class ThreeMedicationDirService extends HandlerExcelData<ThreeMedicationDir> {

	@Override
	public String handlerData(List<List<Object>> objects) {
		if (objects != null && !objects.isEmpty()) {
			List<ThreeMedicationDir> list = Lists.newArrayList();
			PinyinTool tool = new PinyinTool();
			for (List<Object> objs : objects) {
				if(objs.size()<=3){
					continue;
				}
				ThreeMedicationDir limit = new ThreeMedicationDir();
				limit.set("code", objs.get(0));
				limit.set("name", objs.get(1));
				limit.set("type", objs.get(2));
				limit.set("payedGrade", objs.get(3));
				limit.set("price", objs.get(4));
				limit.set("unit", objs.get(5));
				limit.set("standard", objs.get(6));
				if (objs.size() > 7) {
					limit.set("reagentType", objs.get(7));
				}
				if (objs.size() > 8) {
					limit.set("selfPercent", objs.get(8));
				}
				if (objs.size() > 9) {
					limit.set("limitPrice", objs.get(9));
				}
				if (objs.size() > 10) {
					limit.set("heightLimitPrice", objs.get(10));
				}
				if (objs.size() > 11) {
					limit.set("provinceLimitPrice", objs.get(11));
				}
				if (objs.size() > 12) {

					limit.set("cityLimitPrice", objs.get(12));
				}
				if (objs.size() > 13) {
					limit.set("countyLimitPrice", objs.get(13));
				}
				if (objs.size() > 14) {
					limit.set("villageLimitPrice", objs.get(14));
				}
				if (objs.size() > 15) {
					limit.set("bedPrice", objs.get(15));
				}
				if (objs.size() > 16) {
					limit.set("remark", objs.get(16));
				}
				if (objs.get(1) != null) {
					limit.set("nameFirstEn", tool.getFirstSpell((String) objs.get(1)));
					try {
						limit.set("nameAllEn", tool.toPinYin((String) objs.get(1)));
					} catch (BadHanyuPinyinOutputFormatCombination e) {
						e.printStackTrace();
					}
				}
				list.add(limit);
			}
			Db.update("delete from three_medication_dir");
			// 每次执行100条
			Db.batchSave(list, 500);
		}
		return "成功导入" + objects.size() + "条数据，如果数据不对，请检查Excel数据是否有空行或者选了错误的文件";

	}

	@Override
	public File exportExcel(List<ThreeMedicationDir> data) {
		List<String> heads = Lists.newArrayList();
		heads.add("项目编码");
		heads.add("医疗项目名称");
		heads.add("医疗项目分类");
		heads.add("报销等级");
		heads.add("单价");
		heads.add("单位");
		heads.add("规格");
		heads.add("剂型");
		heads.add("自费比例");
		heads.add("限价标志");
		heads.add("最高限价");
		heads.add("省级限价");
		heads.add("市级限价");
		heads.add("县级限价");
		heads.add("乡级限价");
		heads.add("不合理费用标志");
		heads.add("备注");
		// 导出结果excel
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String expPath = "三大目录药品编码表" + sdf.format(new Date()) + ".xls";
		try {
			return ExcelExporter.generateExcelFile(expPath, data, heads.toArray(new String[] {}));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
